<?php

class SchoolController extends Controller {

    public $layout = 'application.modules.admin.views.layouts.main';
    public static $uploadFolder;
    public $model;

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array(''),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform any actions
                'users' => array('@'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
            'postOnly + delete,update', // we only allow deletion via POST request
        );
    }

    public function actionIndex() {
        $criteria = new CDbCriteria();
        if (isset($_GET['id']) && $_GET['id'] < 1000000) {
            $criteria->condition = 'status=' . $_GET['id'];
        } else {
            $criteria->condition = 'status<' . School::STATUS_INVALID;
        }
        $criteria->order = 'create_time DESC';
        $dataProvider = new CActiveDataProvider('School', array(
                    'pagination' => array(
                        'pageSize' => 5,
                    ),
                    'criteria' => $criteria,
                ));
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionCreate() {
        if (isset($_POST['school_id']) && !empty($_POST['school_id'])) {
            $this->model = $this->loadModel($_POST['school_id']);
        } else {
            $this->model = new School();
        }
        $school = $this->model;
        if (isset($_POST['School'])) {
            $school->attributes = $_POST['School'];
            if ($school->validate()) {
                if ($school->isNewRecord)
                    $school->save();
                else
                    $school->update();
                $this->model = $school;
                $this->render('upload', array('school' => $school));
                Yii::app()->end();
            }
        }

        $this->render('create', array('school' => $school));
    }

    public function actionUpload() {
        if (isset($_POST['school_id']) && !empty($_POST['school_id']) && isset($_POST['step'])) {
            $school = $this->loadModel($_POST['school_id']);
            if ($_POST['step'] === '1') {
                if (isset($_FILES['icon'])) {
                    if (self::$uploadFolder === NULL)
                        $this->buildUploadFolder();
                    $iconName = $_FILES['icon']['name'];
                    $destFile = self::$uploadFolder . $iconName;
                    if (move_uploaded_file($_FILES['icon']['tmp_name'], $destFile)) {
                        //mysql_escape_string($content)转义
                        //stripslashes($content)反转义
                        $icon = mysql_escape_string(Yii::app()->baseUrl . '/upload/' . $iconName);
                        School::model()->setIconPath($_POST['school_id'], $icon);
                        $criteria = new CDbCriteria();
                        $criteria->condition = 'status=' . Subject::STATUS_OK;
                        $this->render('subjects', array('school' => $school,
                            'subjects' => Subject::model()->findAll($criteria)));
                        Yii::app()->end();
                    }
                    $this->render('upload', array('school' => $school));
                    Yii::app()->end();
                }
            } else if ($_POST['step'] === '2') {
                $criteria = new CDbCriteria();
                $criteria->condition = 'status=' . Subject::STATUS_OK;
                $this->render('subjects', array('school' => $school,
                    'subjects' => Subject::model()->findAll($criteria)));
                Yii::app()->end();
            }
        }
        $this->redirect('index');
    }

    protected function buildUploadFolder() {
        self::$uploadFolder = Yii::app()->basePath . '/../upload/';
    }

    public function loadModel($id) {
        if ($this->model === NULL) {
            $this->model = School::model()->findByPk($id);
            if ($this->model === null)
                throw new CHttpException(404, 'The requested page does not exist.');
        }
        return $this->model;
    }

    public function actionUpdate() {
        if (isset($_POST['school_id'])) {
            $model = $this->loadModel($_POST['school_id']);
            if (isset($model)) {
                $this->render('create', array(
                    'school' => $model,
                ));
                Yii::app()->end();
            }
        }
        $this->redirect('index');
    }

    public function actionDelete() {
        if (Yii::app()->request->isPostRequest) {
            if (isset($_POST['school_id']) && !empty($_POST['school_id'])) {
                School::model()->deleteSchoolById($_POST['school_id']);
            }
            $this->redirect('index');
        }else
            throw new CHttpException(404, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionPass() {
        if (Yii::app()->request->isPostRequest) {
            if (isset($_POST['school_id']) && !empty($_POST['school_id'])) {
                School::model()->updateByPk($_POST['school_id'], array('status' => School::STATUS_OK));
            }
            $this->redirect('index');
        }else
            throw new CHttpException(404, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionSubjects() {
        if(isset($_POST['subjects']) && !empty($_POST['subjects'])){
            $subjects = implode(',', $_POST['subjects']);
            School::model()->updateByPk($_POST['school_id'], array('subjects' => $subjects));
        }
        $this->redirect('index');
    }

}